[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
s/PATTERN/REPLACEMENT/gieo
Searches a string for a pattern, and if found,
replaces that pattern with the replacement text and
returns the number of substitutions made. Otherwise
it returns false (0). The "g" is optional, and if
present, indicates that all occurrences of the
pattern are to be replaced. The "i" is also
optional, and if present, indicates that matching is
to be done in a case-insensitive manner. The "e" is
likewise optional, and if present, indicates that
the replacement string is to be evaluated as an
expression rather than just as a double-quoted
string. Any non-alphanumeric delimiter may replace
the slashes; if single quotes are used, no interpre-
tation is done on the replacement string (the e
modifier overrides this, however); if backquotes are
used, the replacement string is a command to execute
whose output will be used as the actual replacement
text. If no string is specified via the =~ or !~
operator, the $_ string is searched and modified.
(The string specified with =~ must be a scalar vari-
able, an array element, or an assignment to one of
those, i.e. an lvalue.) If the pattern contains a $
that looks like a variable rather than an end-of-
string test, the variable will be interpolated into
the pattern at run-time. If you only want the pat-
tern compiled once the first time the variable is
interpolated, add an "o" at the end. If the PATTERN
evaluates to a null string, the most recent success-
ful regular expression is used instead. See also
the section on regular expressions. Examples:
s/\bgreen\b/mauve/g; # don't change wintergreen
$path =~ s|/usr/bin|/usr/local/bin|;
s/Login: $foo/Login: $bar/; # run-time pattern
($foo = $bar) =~ s/bar/foo/;
$_ = 'abc123xyz';
s/\d+/$&*2/e; # yields 'abc246xyz'
s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz'
s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz'
s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields
(Note the use of $ instead of \ in the last example.
See section on regular expressions.)
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson